package com.xiaov.brilliard;

import javax.microedition.khronos.opengles.GL10;

import com.xiaov.android.games.my3d.GameView;
import com.xiaov.android.games.my3d.mesh.Shape;
import com.xiaov.math.Vector3D;

public class Cue extends Shape {
	
	final float DEGREE_SPAN = (float)(Math.PI / 180.0f);
	
	public int state = 1;
	
	private int ox = 0;
	private int oy = 30;
	private int oz = 0;
	
	public int offsetX = 0;
	public int offsetY = 0;
	public int offsetZ = 0;
	public float length = 15;
	
	private Ball wb;

	public Cue(float[] vertices, float[] texture, float[] normals,
			short[] indices) {
		super(vertices, texture, normals, indices);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void draw(GL10 gl) {
		// TODO Auto-generated method stub
		if(state == 1){
			gl.glPushMatrix();
			
			//Bind the texture according to the set texture filter
			gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
			//Set the face rotation
			gl.glFrontFace(GL10.GL_CCW);
			
			//Point to our buffers
			gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
			gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
			gl.glNormalPointer(GL10.GL_FLOAT, 0, normalBuffer);
			
			
			//Enable the vertex and color state
			gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
			gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
			gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
			
			gl.glTranslatef(x, y, z);
			gl.glRotatef(rx, 1, 0, 0);
			gl.glRotatef(ry, 0, 1, 0);
			gl.glRotatef(rz, 0, 0, 1);
			gl.glTranslatef(offsetX + ox, offsetY + oy + 32 * (length - 1), offsetZ + oz);
			gl.glScalef(1, length, 1);
			//Draw the vertices as triangles, based on the Index Buffer information
			gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);
			
			//Disable the client state before leaving
			gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
			gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
			gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
			
			gl.glPopMatrix();
		}
	}
	
	public Vector3D getNomalVec(){
		Vector3D v = new Vector3D(0, 10);
		v.setangleX((90 + rz) * DEGREE_SPAN);
		//v.setValue((float)Math.sin(rx * DEGREE_SPAN), (float)Math.cos(ry * DEGREE_SPAN), 0);
		return v;
	}
	
	public int getForce(){
		return offsetY;
	}
	
	public void setBall(Ball wb){
		this.wb = wb;
	}
	
	public void tick(){
		if(wb == null || state != 1){
			return;
		}
		//x = wb.Pos.getX() - GameView.width / 2 + Data.banjin;
		//y = GameView.height / 2- wb.Pos.getY() + Data.banjin;
		x = wb.Pos.getX();
		y = wb.Pos.getY();
	}
	
	public void action(){
		
	}
}
